<script setup>
import { ref } from 'vue'
const visableDrawer = ref(false)
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import {
  AddPointService,
  EditPointService,
  GetPointDetailService
} from '@/api/point'
import { ElMessage } from 'element-plus'
// 默认数据
const defaultForm = {
  pointName: '',
  id: '',
  latitude: '',
  longitude: ''
}

//表单校验规则
const rules = {
  pointName: [
    { required: true, message: '请输入站点名称', trigger: 'blur' },
    {
      pattern: /^\S{1,10}$/,
      message: '站点名称名必须是1-10位的非空字符',
      trigger: 'blur'
    }
  ],
  latitude: [{ required: true, message: '请输入站点维度', trigger: 'blur' }],
  longitude: [{ required: true, message: '请输入站点经度', trigger: 'blur' }]
}

// 准备数据
const formModel = ref({ ...defaultForm })

const open = async (row) => {
  visableDrawer.value = true
  if (row.id) {
    //需要基于row.id发送请求，获取编辑对应的详情数据，进行回显
    const res = await GetPointDetailService(row.id)
    formModel.value = res.data.data
  } else {
    formModel.value = { ...defaultForm } //基于默认的数据，重置form数据
  }
  console.log(row)
}
const formRef = ref()

const onSubmit = async () => {
  await formRef.value.validate()
  const isEdit = formModel.value.id
  if (isEdit) {
    await EditPointService(formModel.value)
    ElMessage.success('修改成功')
    visableDrawer.value = false
    emit('success', 'edit')
  } else {
    delete formModel.value.id
    await AddPointService(formModel.value)
    ElMessage.success('添加成功')
    visableDrawer.value = false
    //通知到父组件，添加成功了
    emit('success', 'add')
  }
}

const emit = defineEmits(['success'])

defineExpose({
  open
})
</script>

<template>
  <el-drawer v-model="visableDrawer" title="添加站点" size="40%">
    <el-form ref="formRef" :model="formModel" :rules="rules" abel-width="100px">
      <el-form-item label="站点名称" prop="pointName">
        <el-input
          placeholder="请输入站点名称"
          v-model="formModel.pointName"
        ></el-input>
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input
          placeholder="请输入站点经度"
          v-model="formModel.longitude"
        ></el-input>
      </el-form-item>
      <el-form-item label="维度" prop="latitude">
        <el-input
          placeholder="请输入站点维度"
          v-model="formModel.latitude"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="onSubmit()" type="primary">提交</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<style lang="scss" scoped>
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }
    .el-upload:hover {
      border-color: var(--el-color-primary);
    }
    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}

.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
